'use strict';
const db = uniCloud.database()
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	
	const res_users = await db.collection('rx-users').doc(event.userId).get()
	
	const res_users_clinic = await db.collection('rx-users-clinic').where({
		userId:event.userId, types: event.types
	}).get()
	
	var filter = { _id: db.command.in(res_users_clinic.data[0].clinic) }
	if ( event.types < 2 ) {
		filter = { _id: db.command.in(res_users_clinic.data[0].clinic), delete: 0 }
	}
	const res_clinic = await db.collection('rx-clinic').where(filter).get()
	
	const clinic_data = res_clinic.data[0]
	const clinic_data_all = res_clinic.data
	
	//返回数据给客户端
	return { data: { 
		name: res_users.data[0].name,
		mobile: res_users.data[0].mobile,
		clinic: clinic_data,
		clinic_data_all: clinic_data_all,
		self: res_users.data[0].self
	}, code: 200, msg: 'ok.' }
};
